package com.binvshe.binvshe.mine.ticket;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.mine.follow.bean.ClubBean;
import com.f2prateek.dart.Dart;
import com.f2prateek.dart.InjectExtra;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/9/5
 */
public class TicketFragment extends SwipeListFragment {

    private int mPage = 0;

    public static TicketFragment newInstance() {
        Bundle args = new Bundle();
        TicketFragment fragment = new TicketFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        Dart.inject(this, getArguments());
        showToolbar();
        setTitle("我的票券");
        getCustomToolbar().addRightButton("已失效", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentUtils.addFragment(getFragmentManager(), R.id.mTicketContainer, LoseTicketFragment.newInstance());
            }
        });
        getCustomToolbar().enableBackButton();
        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        TicketAdapter ticketAdapter = new TicketAdapter(getContext());
        ticketAdapter.setIsLoseTicket(true);
        ticketAdapter.setFragmentManager(getFragmentManager());
        return ticketAdapter;
    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading();
    }

    @Override
    public void onStartRefreshing() {
        mPage = 0;
        getData().clear();
        onStartLoading();
        super.onStartRefreshing();
    }

    @Override
    public void onStartLoading() {
        getTicketList();
    }

    private ListDataRequestListener<TicketEntity> mResponceListener = new ListDataRequestListener<>(this, TicketEntity.class);

    public void getTicketList() {
        if (mPage == 0 || mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
            MyHttpRequest request = new MyHttpRequest(getContext());
            UrlParams urlParams = new UrlParams();
            urlParams.put(ParamKey.PAGE, mPage + "");
            urlParams.put(ParamKey.SIZE, 8 + "");
            request.get(UrlCenter.VOUCHERS, urlParams, mResponceListener);
        }
    }
}
